package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.AdjustInBillCheckMapper;
import com.canaan.business.domain.bill.AdjustInBillCheck;
import com.canaan.business.service.bill.IAdjustInBillCheckService;

/**
 * 调拔入库单清单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@Service
public class AdjustInBillCheckServiceImpl implements IAdjustInBillCheckService {
    @Resource
    private AdjustInBillCheckMapper adjustInBillCheckMapper;

    /**
     * 查询调拔入库单清单
     *
     * @param id 调拔入库单清单主键
     * @return 调拔入库单清单
     */
    @Override
    public AdjustInBillCheck selectAdjustInBillCheckById(Long id) {
        return adjustInBillCheckMapper.selectAdjustInBillCheckById(id);
    }

    /**
     * 查询调拔入库单清单列表
     *
     * @param adjustInBillCheck 调拔入库单清单
     * @return 调拔入库单清单
     */
    @Override
    public List<AdjustInBillCheck> selectAdjustInBillCheckList(AdjustInBillCheck adjustInBillCheck) {
        return adjustInBillCheckMapper.selectAdjustInBillCheckList(adjustInBillCheck);
    }

    /**
     * 新增调拔入库单清单
     *
     * @param adjustInBillCheck 调拔入库单清单
     * @return 结果
     */
    @Override
    public int insertAdjustInBillCheck(AdjustInBillCheck adjustInBillCheck) {
        if(StringUtils.isBlank(adjustInBillCheck.getNo())){
            adjustInBillCheck.setNo(TableNoUtil.getNo(AdjustInBillCheck.class));
        }
        adjustInBillCheck.setCreateBy(SecurityUtils.getUsername());
        adjustInBillCheck.setCreateTime(DateUtils.getNowDate());
        return adjustInBillCheckMapper.insertAdjustInBillCheck(adjustInBillCheck);
    }

    /**
     * 批量新增调拔入库单清单
     *
     * @param adjustInBillCheckList 调拔入库单清单列表
     * @return 结果
     */
    @Override
    public int batchInsertAdjustInBillCheck(List<AdjustInBillCheck> adjustInBillCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustInBillCheck adjustInBillCheck : adjustInBillCheckList){
            if(StringUtils.isBlank(adjustInBillCheck.getNo())){
                adjustInBillCheck.setNo(TableNoUtil.getNo(AdjustInBillCheck.class));
            }
            adjustInBillCheck.setCreateBy(username);
            adjustInBillCheck.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<adjustInBillCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustInBillCheckList.size()) {
                endIndex = adjustInBillCheckList.size();
            }
            insertNum = insertNum + adjustInBillCheckMapper.batchInsertAdjustInBillCheck(adjustInBillCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改调拔入库单清单
     *
     * @param adjustInBillCheck 调拔入库单清单
     * @return 结果
     */
    @Override
    public int updateAdjustInBillCheck(AdjustInBillCheck adjustInBillCheck) {
        adjustInBillCheck.setUpdateBy(SecurityUtils.getUsername());
        adjustInBillCheck.setUpdateTime(DateUtils.getNowDate());
        return adjustInBillCheckMapper.updateAdjustInBillCheck(adjustInBillCheck);
    }

    /**
     * 批量修改调拔入库单清单
     *
     * @param adjustInBillCheckList 调拔入库单清单列表
     * @return 结果
     */
    @Override
    public int batchUpdateAdjustInBillCheck(List<AdjustInBillCheck> adjustInBillCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustInBillCheck adjustInBillCheck : adjustInBillCheckList){
            adjustInBillCheck.setUpdateBy(username);
            adjustInBillCheck.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<adjustInBillCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustInBillCheckList.size()) {
                endIndex = adjustInBillCheckList.size();
            }
            updateNum = updateNum + adjustInBillCheckMapper.batchUpdateAdjustInBillCheck(adjustInBillCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除调拔入库单清单
     *
     * @param ids 需要删除的调拔入库单清单主键集合
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillCheckByIds(Long[] ids) {
        return adjustInBillCheckMapper.deleteAdjustInBillCheckByIds(ids);
    }

    /**
     * 删除调拔入库单清单信息
     *
     * @param id 调拔入库单清单主键
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillCheckById(Long id) {
        return adjustInBillCheckMapper.deleteAdjustInBillCheckById(id);
    }

    /**
     * 查询调拔入库单清单
     *
     * @param no 编号
     * @return 调拔入库单清单
     */
    @Override
    public AdjustInBillCheck selectAdjustInBillCheckByNo(String no) {
        return adjustInBillCheckMapper.selectAdjustInBillCheckByNo(no);
    }

    /**
     * 批量删除调拔入库单清单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillCheckByNos(String[] nos) {
        return adjustInBillCheckMapper.deleteAdjustInBillCheckByNos(nos);
    }

    /**
     * 删除调拔入库单清单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteAdjustInBillCheckByNo(String no) {
        return adjustInBillCheckMapper.deleteAdjustInBillCheckByNo(no);
    }
}
